{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit TestEditor;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  ExtCtrls, StdCtrls, ActnList, Shape, QuestionList, SectionTree, Scene,
  VisualUtils, superobject, QuestionViewer, MiscUtils, QuestionEditors;

type
  TTestEditorFrame = class(TShapeFrame)
    actSetViewMode: TAction;
    actSetEditMode: TAction;
    actRefreshQuestion: TAction;
    actResetAnswer: TAction;
    actShowCorrectAnswer: TAction;
    alQuestionViewer: TActionList;
    bvlTop: TBevel;
    ilQuestionViewer: TImageList;
    lblError: TLabel;
    pnlSections: TPanel;
    pnlQuestions: TPanel;
    pbxPercentCorrect: TPaintBox;
    pnlLeft: TPanel;
    pnlQuestion: TPanel;
    pnlTop: TPanel;
    spHorizontal: TSplitter;
    spVertical: TSplitter;
    tbSetEditMode: TToolButton;
    tbRefreshQuestion: TToolButton;
    tbResetAnswer: TToolButton;
    tbrMode: TToolBar;
    tbrQuestionViewer: TToolBar;
    tbShowCorrectAnswer: TToolButton;
    tbSetViewMode: TToolButton;
    procedure actRefreshQuestionExecute(Sender: TObject);
    procedure actRefreshQuestionUpdate(Sender: TObject);
    procedure actResetAnswerExecute(Sender: TObject);
    procedure actResetAnswerUpdate(Sender: TObject);
    procedure actSetEditModeExecute(Sender: TObject);
    procedure actSetViewModeExecute(Sender: TObject);
    procedure actShowCorrectAnswerExecute(Sender: TObject);
    procedure actShowCorrectAnswerUpdate(Sender: TObject);
    procedure pbxPercentCorrectPaint(Sender: TObject);
  private
    FQuestionList: TQuestionListFrame;
    FSectionTree: TSectionTreeFrame;
    FQuestionEditor: TShapeFrame;
    FQuestionViewer: TQuestionViewerFrame;
    FSelectQuestionViewer: Boolean;
    function QuestionListEditorId: TShapeId;
    function QuestionEditorId: TShapeId;
    function ShownQuestionEditorId: TShapeId;
    function SetViewMode(ViewMode: Boolean): Boolean;
    procedure QuestionViewerEvaluateAnswer(Sender: TObject);
    procedure DisplayEvaluationResult;
    { private declarations }
  public
    procedure AfterConstruction; override;
    destructor Destroy; override;
    procedure Render;
    { public declarations }
  end; 

implementation

{$R *.lfm}
{$R *.rc}

{ TTestEditorFrame }

procedure TTestEditorFrame.AfterConstruction;
begin
  inherited;
  alQuestionViewer.OnExecute := Scene.ActionListExecuteFilter;
  LoadActionImages(alQuestionViewer);

  FSectionTree := TSectionTreeFrame.Create(Shape.S['sectionTreeEditor'], Dispatcher, pnlSections);
  SetFixedSizeConstraints(pbxPercentCorrect,
    pbxPercentCorrect.Canvas.TextWidth('___100%___'),
    GetPreferredControlSize(tbrMode).cy);

  tbSetEditMode.Constraints.MinWidth := ScalePixels(60);
  tbSetViewMode.Constraints.MinWidth := tbSetEditMode.Constraints.MinWidth;
end;

destructor TTestEditorFrame.Destroy;
begin
  FreeAndNil(FQuestionList);
  FreeAndNil(FSectionTree);
  FreeAndNil(FQuestionEditor);
  FreeAndNil(FQuestionViewer);
  inherited;
end;

procedure TTestEditorFrame.pbxPercentCorrectPaint(Sender: TObject);
var
  c: TCanvas;
  r, r2: TRect;
  ts: TTextStyle;
begin
  c := pbxPercentCorrect.Canvas;
  r := InflateRectangle(pbxPercentCorrect.ClientRect, 0, -2);
  c.Pen.Color := $808080;
  c.Rectangle(r);

  if (FQuestionViewer <> nil) and not FQuestionViewer.IsEmpty
    and not FQuestionViewer.EvaluationFailed then
  begin
    r2 := InflateRectangle(r, -1, -1);
    r2.Right := r2.Left + Round((r2.Right - r2.Left) * FQuestionViewer.PartRight);
    c.Brush.Color := $2aff7f;
    c.FillRect(r2);

    ts := c.TextStyle;
    ts.Alignment := taCenter;
    ts.Layout := tlCenter;
    c.TextRect(r, 0, 0, IntToStr(Round(FQuestionViewer.PartRight*100)) + '%', ts);
  end;
end;

procedure TTestEditorFrame.actShowCorrectAnswerUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (FQuestionViewer <> nil) and FQuestionViewer.CanShowCorrectAnswer;
end;

procedure TTestEditorFrame.actShowCorrectAnswerExecute(Sender: TObject);
begin
  FQuestionViewer.ShowCorrectAnswer;
end;

procedure TTestEditorFrame.actResetAnswerUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (FQuestionViewer <> nil) and FQuestionViewer.CanResetAnswer;
end;

procedure TTestEditorFrame.actSetEditModeExecute(Sender: TObject);
begin
  SetViewMode(FALSE);
end;

procedure TTestEditorFrame.actSetViewModeExecute(Sender: TObject);
begin
  if SetViewMode(TRUE) then
    FSelectQuestionViewer := TRUE
  else if FQuestionViewer <> nil then
    FQuestionViewer.Select;
end;

procedure TTestEditorFrame.actResetAnswerExecute(Sender: TObject);
begin
  FQuestionViewer.ResetAnswer;
end;

procedure TTestEditorFrame.actRefreshQuestionUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (FQuestionViewer <> nil) and FQuestionViewer.CanRefreshQuestion;
end;

procedure TTestEditorFrame.actRefreshQuestionExecute(Sender: TObject);
begin
  FQuestionViewer.RefreshQuestion;
  FSelectQuestionViewer := TRUE;
end;

function TTestEditorFrame.QuestionListEditorId: TShapeId;
begin
  Result := Shape.S['questionListEditor'];
end;

function TTestEditorFrame.QuestionEditorId: TShapeId;
begin
  Result := Shape.S['questionEditor'];
end;

function TTestEditorFrame.ShownQuestionEditorId: TShapeId;
begin
  if FQuestionEditor = nil then
    Result := ''
  else
    Result := FQuestionEditor.Id;
end;

function TTestEditorFrame.SetViewMode(ViewMode: Boolean): Boolean;
var
  Input: ISuperObject;
begin
  Result := ViewMode <> Shape.B['viewMode'];
  if Result then
  begin
    Input := SO;
    Input.B['viewMode'] := ViewMode;
    Dispatcher.PerformAction(Id, 'setViewMode', Input);
  end;
end;

procedure TTestEditorFrame.QuestionViewerEvaluateAnswer(Sender: TObject);
begin
  DisplayEvaluationResult;
end;

procedure TTestEditorFrame.DisplayEvaluationResult;
begin
  pbxPercentCorrect.Invalidate;
  lblError.Caption := ' ' + FQuestionViewer.EvaluationErrorMessage + ' ';
  lblError.Hint := FQuestionViewer.EvaluationErrorMessage;
  lblError.Visible := FQuestionViewer.EvaluationFailed;
end;

procedure TTestEditorFrame.Render;
var
  Kind: TKind;
  ViewMode, SelectQuestionEditor, QuestionEditorRendered: Boolean;
begin
  UpdateShape;

  LockRedraw(pnlQuestions);
  try
    if (FQuestionList <> nil) and (FQuestionList.Id <> QuestionListEditorId) then
      FreeAndNil(FQuestionList);
    if FQuestionList = nil then
      FQuestionList := TQuestionListFrame.Create(QuestionListEditorId, Dispatcher, pnlQuestions);
    SelectQuestionEditor := FQuestionList.QuestionAdded;
    FQuestionList.Render;
  finally
    UnlockRedraw(pnlQuestions);
  end;

  FSectionTree.Render;

  ViewMode := Shape.B['viewMode'];
  if ViewMode then
    tbSetViewMode.Down := TRUE
  else
    tbSetEditMode.Down := TRUE;
  tbrQuestionViewer.Visible := ViewMode;
  pbxPercentCorrect.Visible := ViewMode;

  if ViewMode then
  begin
    if (FQuestionViewer = nil) or not (FQuestionViewer.UpToDate) then
    begin
      LockRedraw(pnlQuestion);
      try
        FreeAndNil(FQuestionEditor);
        FreeAndNil(FQuestionViewer);
        FQuestionViewer := TQuestionViewerFrame.Create(Shape.S['questionViewer'],
          Dispatcher, pnlQuestion);
        DisplayEvaluationResult;
        FQuestionViewer.OnEvaluateAnswer := QuestionViewerEvaluateAnswer;
      finally
        UnlockRedraw(pnlQuestion);
      end;
    end;
  end
  else
  begin
    lblError.Visible := FALSE;
    QuestionEditorRendered := FALSE;
    if (FQuestionViewer <> nil) or (QuestionEditorId <> ShownQuestionEditorId) then
    begin
      LockRedraw(pnlQuestion);
      try
        FreeAndNil(FQuestionEditor);
        FreeAndNil(FQuestionViewer);
        if QuestionEditorId <> '' then
        begin
          Kind := Scene.GetKind(Scene.Shapes[QuestionEditorId]);
          FQuestionEditor := QuestionEditorRegistry.Find(Kind).Create(
            QuestionEditorId, Dispatcher, pnlQuestion);
          (FQuestionEditor as IQuestionEditor).Render;
          QuestionEditorRendered := TRUE;
        end;
      finally
        UnlockRedraw(pnlQuestion);
      end;
    end;

    if FQuestionEditor <> nil then
    begin
      if not QuestionEditorRendered then
        (FQuestionEditor as IQuestionEditor).Render;
      if SelectQuestionEditor then
        FQuestionEditor.SelectFirst;
    end;
  end;

  if FSelectQuestionViewer and (FQuestionViewer <> nil) then
    FQuestionViewer.Select;
  FSelectQuestionViewer := FALSE;
end;

end.

